petitcomputerfandomcom-20200214-history
User blog:Swaxhog/Apple Chomp - My First Program
I have to thank the 'Flappy Bird' mania for introducing me to Petit Computer. I recently purchased a 3DS XL and when I read a news article about the Flappy Bird written for the DS using BASIC I just had to check out PTC. While I have some programming experience from many years ago starting with an Apple computer using Applesoft BASIC, my programming has mainly been in business or application development using Delphi. Even that stopped almost 10 years ago. So I know the concepts I just need to learn the syntax. Another thing I've never done is actually write a game. With scoring and rules and music. Luckily PTC handles a lot of that for me. So with a few hours free this day, I started learning some basic things like drawing on the screen at a specific coordinate, playing music and beeps, checking 'collisions' and trying to figure out how to add a timer (I think it's a little buggy when the minute flips before the math is calculated) I decided to start simple and just work with text. This little game called APPLE CHOMP generates a random maze and you move your character to chomp on apples of various colors. You have 60 seconds and get +2 seconds for each apple. If you hit a wall, you lose one of your 3 men. The game is over when the timer runs out or you die 3 times. Sometimes you or the apple are boxed in. Life is tough! Apple Chomp ver 1. Notice that juicy red apple? Yum! I learned a lot in this simple program and for any beginners it might be helpful to see how easy it is to do things like keep score, move around a screen, and even a simple timer. Here is the source code. ' APPLE CHOMP ' USE D-PAD TO MOVE THROUGH FOREST ' HUNTING FOR APPLES TO CHOMP ' YOU HAVE 60 SECONDS TO CHOMP ' AS MANY APPLES AS YOU CAN ' EACH APPLE GIVES YOU +2 SECONDS ' Written by swaxhog C$=CHR$(244) LIVES=3 : APPLES=0 : COUNTDOWN = 60: C2=60 TMREAD(TIME$),H,M,S : CLOCK1= M*60+S @STARTUP GOSUB @SETUP @MAIN WAIT 3 IF BUTTON() 1 THEN GOSUB @MUP IF BUTTON() 2 THEN GOSUB @MDOWN IF BUTTON() 4 THEN GOSUB @MLEFT IF BUTTON() 8 THEN GOSUB @MRIGHT IF BUTTON() 64 THEN END ' CHECK TO SEE IF YOU HIT A WALL OR THE APPLE LOCATE X,Y IF CHKCHR(X,Y) 151 THEN @DEAD IF CHKCHR(X,Y) 233 THEN @WIN PRINT C$ 'CLOCK COUNT DOWN. KEEPS TRACK EACH WAVE TMREAD(TIME$),H,M,S : CLOCK2= M*60+S : COUNTDOWN=C2 - (CLOCK2-CLOCK1) LOCATE 20,0: PRINT "TIMER="; COUNTDOWN;CHR$(151) IF (COUNTDOWN <10) AND (SETHURRY 0) THEN GOSUB @HURRYUP IF COUNTDOWN 0 THEN @DEAD VSYNC 5 GOTO @MAIN ' YOU'RE A WIENER @WIN APPLES=APPLES+1 LOCATE 2,0: PRINT CHR$(244);"=";LIVES LOCATE 8,0: PRINT CHR$(233);"=";APPLES BGMSTOP : BEEP 3 : WAIT 20 ' SET VAR TO KEEP COUNTDOWN TIMER FROM NEW POSITION ' PLUS ADD 2 SECONDS TO CLOCK C2=COUNTDOWN+2 : TMREAD(TIME$),H,M,S : CLOCK1= M*60+S GOTO @STARTUP @DEAD BGMSTOP BEEP 6 LIVES = LIVES -1 IF (LIVES 0) OR (COUNTDOWN 0) THEN @DEADDEAD LOCATE 2,0: PRINT CHR$(244);"=";LIVES LOCATE 8,0: PRINT CHR$(233);"=";APPLES LOCATE 5,10 : PRINT "######################" LOCATE 5,11 : PRINT "## S P L A T ! ##" LOCATE 5,12 : PRINT "## A=AGAIN ##" LOCATE 5,13 : PRINT "######################" @ENDDLOOP IF BUTTON() 64 THEN END IF BUTTON()!=16 THEN @ENDDLOOP C2=COUNTDOWN : TMREAD(TIME$),H,M,S : CLOCK1= M*60+S GOTO @STARTUP @DEADDEAD LOCATE 2,0: PRINT CHR$(244);"=";LIVES LOCATE 8,0: PRINT CHR$(233);"=";APPLES LOCATE 5,10 : PRINT "######################" LOCATE 5,11 : PRINT "## GAME OVER ##" LOCATE 5,12 : PRINT "## A=AGAIN X=QUIT ##" LOCATE 5,13 : PRINT "######################" @ENDDDLOOP IF BUTTON() 16 THEN @DEADSETUP IF BUTTON() 64 THEN END GOTO @ENDDDLOOP @DEADSETUP COUNTDOWN = 60: C2=60 : TMREAD(TIME$),H,M,S : CLOCK1= M*60+S LIVES = 3 : APPLES=0 GOTO @STARTUP ' SET UP THE SCREEN & INITIALIZE VARIABLES @SETUP CLS BGMPLAY 1 IF COUNTDOWN < 10 THEN BGMPLAY 2 FOR I = 0 TO 31 : LOCATE I,22 : PRINT CHR$(151) : NEXT FOR I = 0 TO 31 : LOCATE I,0 : PRINT CHR$(151) : NEXT LOCATE 12,22 : PRINT "APPLE CHOMP" LOCATE 2,0: PRINT CHR$(244);"=";LIVES LOCATE 8,0: PRINT CHR$(233);"=";APPLES FOR I = 0 TO 150 : LOCATE RND(31),RND(20)+1 : PRINT CHR$(151) : NEXT COLOR RND(11)+3: LOCATE RND(31),RND(20)+1 : PRINT CHR$(233) : COLOR 0 SETHURRY = 0: X=0 : Y=12 : LOCATE X,Y: PRINT C$;" " RETURN 'FLAG TO TOGGLE HURRY UP MUSIC @HURRYUP BGMPLAY 2 SETHURRY=1 RETURN ' MOVE ROUTINES @MLEFT LOCATE X,Y : PRINT " " : X=X-1: IF X<0 THEN X=0 RETURN @MRIGHT LOCATE X,Y : PRINT " " : X=X+1:IF X>31 THEN X=31 RETURN @MUP LOCATE X,Y : PRINT " " : Y=Y-1: IF Y<1 THEN Y=1 RETURN @MDOWN LOCATE X,Y : PRINT " " : Y=Y+1: IF Y>20 THEN Y=20 RETURN Category:Blog posts